<?php
namespace app\index\controller;
use app\index\Controller;
use think\Validate;
use think\Db;
/**
 * @title 登录注册（模块01）
 * @description 接口说明
 * @groupremark 已使用错误码： 1010001 用户未注册,1010002 登录失败,1010003 校验失败,1010004 授权失败
 * @group 用户
 * @header name:model_id   default:01 desc:模块id
 */
class Login extends Controller
{
    /**
     * @var array 白名单方法，如果设置会覆盖黑名单方法，只允许白名单方法能正常访问
     */
    protected static $allowList = ['index','register','code','updatepwd','updatephone'];
    /**
     * @title 小程序登录
     * @description 接口说明
     * @author 梁敏
     * @url /index/login/index
     * @method ~
     * @param name:code type:string require:1
     * @return token:token
     * @return wx_user:用户信息@!
     * @wx_user nickname:名称 user_id:用户id
     * @return error_id: 1010001 用户未注册,1010002 登录失败
     */
    public function index()
    {
        $_data = input();
        $validate = new Validate([
            'code' => 'require',
        ]);
        if (!$validate->check($_data)) {
            $msg['msg']=$validate->getError();
            return $this->error($msg);
        }
        $weixin_config=  config("weixin_config");
        $Miniprogram =new \WeMini\Crypt($weixin_config);
        //登录凭证校验
        $Session = $Miniprogram->session($_data['code']);

        if($Session){
            $wx_user=Db::name("weixin_user")->where("openid",$Session['openid'])->find();
            if(!$wx_user){
                $msg['token']=false;
                $msg['register']=false;
                $msg['msg']="用户未注册";
                $msg['error_id']="1010001";

                return $this->error($msg);
            }
            session('config_key',$_data['config_key']);
            session('session_key',$Session['session_key']);
            session('openid',$Session['openid']);
            session('unionid',$Session['unionid']);

            $msg['wx_user']=$wx_user;
            $msg['token']=session_id();
            $msg['msg']="登录成功";
            return $this->success($msg);
        }else{
            $msg['token']=false;
            $msg['msg']="登录失败";
            $msg['error_id']="1010002";
            return $this->error($msg);
        }

    }

    /**
     * @title 小程序注册
     * @description 接口说明
     * @author 梁敏
     * @url /index/login/register
     * @method ~
     * @param name:code type:string require:1
     * @param name:encryptedData type:string require:1
     * @param name:iv type:string require:1
     * @return token:token
     * @return wx_user:用户信息@!
     * @wx_user nickname:名称 user_id:用户id
     * @return error_id: 1010003 校验失败,1010004 授权失败
     */
    public function register()
    {
        $_data = input();
        $validate = new Validate([
            'code' => 'require',
            'encryptedData' => 'require',
            'iv' => 'require',

        ]);
        if (!$validate->check($_data)) {
            $msg['register']=false;
            $msg['token']=false;
            $msg['msg']=$validate->getError();
            $msg['error_id']="1010003";

            return $this->error($msg);
        }
        $weixin_config=  config("weixin_config");
        $Miniprogram =new \WeMini\Crypt($weixin_config);

        $user_info = $Miniprogram->userInfo($_data['code'], $_data['iv'],$_data['encryptedData']);

        if($user_info){

            $map['openid']=$user_info['openid'];
            $wx_user=Db::name("WeixinUser")->where($map)->find();
            session('config_key',$_data['config_key']);
            session('session_key',$user_info['session_key']);
            session('openid',$user_info['openid']);
            session('unionid',$user_info['unionid']);


            if($wx_user){
                $msg['token']=session_id();
                $msg['register']=true;
                $msg['msg']="用户已注册";
                $msg['wx_user']=$wx_user;
                return $this->success($msg);
            }else{

                $wx_user['openid']=$user_info['openId'];
                $wx_user['nickname']=$user_info['nickName'];
                $wx_user['sex']=$user_info['gender'];
                $wx_user['country']=$user_info['country'];
                $wx_user['province']=$user_info['province'];
                $wx_user['city']=$user_info['city'];
                $wx_user['headimgurl']=$user_info['avatarUrl'];
                $wx_user['language']=$user_info['language'];




                $user_id=model("WeixinUser")->wx_add_user($wx_user);
                $map['user_id']=$user_id;
                $wx_user=Db::name("WeixinUser")->where($map)->find();
                $msg['token']=session_id();
                $msg['register']=true;
                $msg['wx_user']=$wx_user;
                $msg['msg']="注册成功";
                return $this->success($msg);

            }

        }else{
            $msg['register']=false;
            $msg['token']=false;
            $msg['msg']="授权失败";
            $msg['error_id']="1010004";

            return $this->error($msg);
        }



    }
}
